package com.konsung.greendaoUtil;

import android.content.Context;
import android.text.TextUtils;

import com.konsung.bean.MeasureDataBean;
import com.konsung.bean.PatientBean;
import com.konsung.util.SpUtils;
import com.konsung.util.UiUtils;
import com.konsung.util.constant.GlobalConstant;

import org.greenrobot.greendao.query.LazyList;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 类功能：数据库一些常用的方法
 */

public class DBUtitl {
    /**
     * 查询当前居民
     * @param context 上下文
     * @return 当前居民
     */
    public static PatientBean getCurrentPatient(Context context) {
        String idCard = SpUtils.getSp(context, GlobalConstant.APP_CONFIG,
                GlobalConstant.CURRENT_PATIENT, "");
        if (!TextUtils.isEmpty(idCard)) {
            PatientBeanDao patientDao = GreenDaoManager.getPatientDao(context);
            List<PatientBean> list = patientDao.queryBuilder().where(PatientBeanDao.Properties
                    .Idcard.eq(idCard))
                    .build()
                    .forCurrentThread().list();
            if (list != null && list.size() > 0) {
                return list.get(0);
            }
        }
        return null;
    }

    /**
     * 查询当前居民最新的测量记录
     * @param context 上下文
     * @return 最新的测量记录
     */
    public static MeasureDataBean getCurrentPatientNewestMeasure(Context context) {
        String idCard = SpUtils.getSp(context, GlobalConstant.APP_CONFIG,
                GlobalConstant.CURRENT_PATIENT, "");
        if (!TextUtils.isEmpty(idCard)) {
            MeasureDataBeanDao measureDao = GreenDaoManager.getMeasureDao(context);
            List<MeasureDataBean> query = measureDao
                    .queryBuilder()
                    .orderDesc(MeasureDataBeanDao.Properties.MeasureTime)
                    .where(MeasureDataBeanDao.Properties.Idcard.eq(idCard))
                    .build()
                    .forCurrentThread()
                    .list();
            if (query != null && query.size() > 0) {
                return query.get(0);
            }
        }
        return null;
    }
    /**
     * 查询当前居民当天未上传的测量数据
     * @param context 上下文
     * @return 最新的测量记录
     */
    public static MeasureDataBean getCurrentPatientNotUploadMeasureData(Context context) {
        try {
            String idCard = SpUtils.getSp(context, GlobalConstant.APP_CONFIG,
                    GlobalConstant.CURRENT_PATIENT, "");
            if (!TextUtils.isEmpty(idCard)) {
                Date date = new Date();
                String format = UiUtils.getDateFormat(UiUtils.DateState.SHORT).format(date);
                Date startDate = UiUtils.getDateFormat(UiUtils.DateState.SHORT).parse(format);
                MeasureDataBeanDao measureDao = GreenDaoManager.getMeasureDao(context);
                List<MeasureDataBean> query = measureDao
                        .queryBuilder()
                        .orderDesc(MeasureDataBeanDao.Properties.MeasureTime)
                        .where(MeasureDataBeanDao.Properties.Idcard.eq(idCard))
                        .where(MeasureDataBeanDao.Properties.MeasureTime.ge(startDate.getTime()))
                        .where(MeasureDataBeanDao.Properties.UploadFlag.eq(false))
                        .build()
                        .forCurrentThread()
                        .list();
                if (query != null && query.size() > 0) {
                    return query.get(0);
                }
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }
    /**
     * 查询当前居民最新的测量记录
     * @param context 上下文
     * @return 最新的测量记录
     */
    public static MeasureDataBean getNewestMeasure(Context context) {
        MeasureDataBeanDao measureDao = GreenDaoManager.getMeasureDao(context);
        LazyList<MeasureDataBean> query = measureDao
                .queryBuilder()
                .offset(0)
                .limit(1)
                .orderDesc(MeasureDataBeanDao.Properties.MeasureTime)
                .build()
                .forCurrentThread()
                .listLazy();
        if (query != null && query.size() > 0) {
            MeasureDataBean measureDataBean = query.get(0);
            query.close();
            return measureDataBean;
        } else if (query != null) {
            query.close();
        }
        return null;
    }

    /**
     * 根据身份证查询居民
     * @param context 上下文
     * @param idcard 身份证
     * @return 查询到的居民
     */
    public static List<PatientBean> queryIdCard(Context context, String idcard) {
        PatientBeanDao patientDao = GreenDaoManager.getPatientDao(context);
        List<PatientBean> patientBeanList = patientDao.queryBuilder()
                .where(PatientBeanDao.Properties.Idcard.eq(idcard))
                .build()
                .forCurrentThread()
                .list();
        if (patientBeanList != null && patientBeanList.size() > 0) {
            //身份证号码已存在
            return patientBeanList;
        }
        return new ArrayList<>();
    }

    /**
     * 更新测量数据
     * @param context 上下文
     * @param measureDataBean 测量数据
     */
    public static void updataMeasure(Context context, MeasureDataBean measureDataBean) {
        GreenDaoManager.getMeasureDao(context).update(measureDataBean);
    }

    /**
     * 更新居民
     * @param context 上下文
     * @param bean 居民数据
     */
    public static void updataPatient(Context context, PatientBean bean) {
        GreenDaoManager.getPatientDao(context).update(bean);
    }

    /**
     * 查询该表全部数据,倒序
     * @param context 上下文引用
     * @return 集合
     */
    public static List<PatientBean> queryAll(Context context) {
        return GreenDaoManager.getPatientDao(context).queryBuilder().orderDesc(PatientBeanDao
                .Properties.AddDate).list();
    }
}
